<?php
class CArchiveModel extends CommonModel{
    protected $tableName="arctiny";
    
    //获取一条文档
    function GetOneArchive($aid,$typeid=0){
        $where=array(
            "arc.id"=>$aid,
        );
        if($typeid > 0){
            $where['arc.typeid']=$typeid;
        }
        //取出主表数据
        $maindata=$this->alias("arc")->field("arc.*,c.addtable,t.name as typename")->join("inner join __CHANNEL__ as c on c.id=arc.channelid")->join("inner join __ARCTYPE__ as t on t.id=arc.typeid")->where($where)->limit("1")->find();
        if(!$maindata){
            return NULL;
        }
        //取附加表数据
        $adddata=$this->table($maindata['addtable'])->field(true)->where("aid='{$aid}'")->limit(1)->find();
        //合并数据
        if(!empty($adddata)){
            $data=array_merge($maindata,$adddata);
        }else{
            $data=$maindata;
        }
        //返回数据
        return $data;
    }
    /**
     * 获取文档列表
     * @param mixed $typeid 分类id，多个id用,隔开
     * @param string $order 排序方式：字段名|排序方式，多重排序用","隔开，如：id|DESC,totalcc|ASC……
     * @param string $status 文档状态，-1是回收站，0是待审核，1表示正常
     * @param string $field 指定字段，字段名以","隔开，默认为true，例如：id,title,typeid
     * @param bool $ctype 是否自动查询分类下的子分类文档
     */
    function GetArcList($typeid="0",$order='id|desc',$flag="",$channelid="1",$status="1",$field=TRUE,$ctype=TRUE){
        //先取缓存
        $cache_key=md5("arclist_".implode("|",  func_get_args()));
        $cache=Cache::getInstance("File",GetCacheOptions('arclist'));
        $cache_data=$cache->get($cache_key);
        if($cache_data!==FALSE){
            return $cache_data;
        }
        $map=$this->listMap($typeid,$status,$ctype);
        $map['channelid']=$channelid;//频道
        if(!empty($flag)){
            $flags=explode(",",$flag);
            foreach($flags as $v){
                $where_flag[]='%'.$v.'%';
            }
            unset($v,$flags);
            $map['flag']=array("like",$where_flag,"OR");
        }
        $order=$this->listOrder($order);
        $list=$this->field(TRUE)->where($map)->order($order)->select();
        //附加字段处理
        $list=$this->listAddonField($list);
        $cache->set($cache_key,$list);
        return $list;
    }
    //生成文章列表查询条件
    function listMap($typeid="0",$status="1",$ctype=true){
        static $_listmap=array();
        $key=$typeid."_".$status."_".(int)$ctype;
        if(isset($_listmap[$key])){
            return $_listmap[$key];
        }
        $cids=array();
        $map=array();
        if(!is_numeric($typeid)){
            $typeids=explode(",",$typeid);
            foreach($typeids as $v){
                if(is_numeric($v)){
                    $cids[]=$v;
                    //获取子分类id
                    if($ctype){
                        $v_cids=D('CArctype')->GetChildTypeIds($v);
                        $cids=array_merge($cids,$v_cids);
                    }
                }
            }
        }else{
            if($typeid!=0){
                $cids[]=$typeid;
                //获取子分类id
                if($ctype){
                    $v_cids=D('CArctype')->GetChildTypeIds($typeid);
                    $cids=array_merge($cids,$v_cids);
                }
            }
        }
        if(!empty($cids)){
            $map["typeid"]=array("in",$cids);
        }
        $map['status']=(string)$status;
        $_listmap[$key]=$map;
        return $_listmap[$key];
    }
    
    //生成文章列表排序方式,支持多重排序
    function listOrder($order="id"){
        if(empty($order)){
            return "";
        }
        $orderarr=array();
        $order1=explode(",",$order);
        foreach($order1 as $v){
            list($field,$sort)=explode("|",$v,2);
            $sort=empty($sort)?"DESC":$sort;
            $orderarr[$field]=$sort;
        }
        return $orderarr;
    }
    
    //文章列表附加字段处理
    function listAddonField($list){
        $carctypeM=D('CArctype');
        $typelist=$carctypeM->GetTypeListWithId();
        foreach($list as $k=>$eachlist){
            if(isset($typelist[$eachlist['typeid']])){
                //添加typename字段
                $list[$k]['typename']=$typelist[$eachlist['typeid']]['name'];
                //添加typealias字段
                $list[$k]['typealias']=$typelist[$eachlist['typeid']]['alias'];
                //添加typeurl字段
                $list[$k]['typeurl']=$carctypeM->GetTypeUrl($eachlist['typeid']);
                //添加arcurl字段
                $list[$k]['arcurl']=$this->GetArcUrl($list[$k]['id'],$list[$k]);
            }
            
            
            //添加arcurl字段
        }
        return $list;
    }
    
    function GetArcUrl($aid,$vars){
        $url="";
        $vars['aid'] = isset($vars['aid'])?$vars['aid']:$vars['id'];
        $vars['said'] = alphaID($aid);
        $urlrule = D('CUrlrule')->GetUrlRule("archive/index");
        if(!empty($urlrule)){
            $url = anchor($urlrule['rule'], $vars);
        }else{
            $url = _U("archive/index?aid=".$aid); 
        }
        return $url;
    }
}
